<!DOCTYPE html>
<html lang="en">
	<head>
		<title>three.js webgl - loaders - OBJ MTL loader</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<style>
			body {
				font-family: Monospace;
				background-color: #ffffff;
				color: #ffffff;
				margin: 0px;
				overflow: hidden;
			}
			#info {
				color: #fff;
				position: absolute;
				top: 10px;
				width: 100%;
				text-align: center;
				display:block;
			}
		</style>
	</head>

	<body bgcolor="#f0f8ff">

		<div id="info">
			<a href="http://threejs.org" target="_blank">three.js</a> - OBJMTLLoader test
			<table width="485" border="1" cellspacing=0 cellpadding=5>
				<tr>
					<td align="left" height="105" ondragenter="return false" ondragover="return false" ondrop="dropIt(event)">
						<output id="thumbs"></output>
					</td>
				</tr>
				<tr>
					<td align="center">
						<p>Drag & drop or choose images from your local file system: <input type="file" id="input" size="10" multiple="true" onchange="filesSelected(this.files)" /></p>
					</td>
				</tr>
			</table>
			<canvas id="myCanvas"></canvas>
		</div>

		<script src="three.js"></script>
		<script src="OrbitControls.js"></script>
		<script src="TrackballControls.js"></script>
		<script src="MTLLoader.js"></script>
		<script src="OBJMTLLoader.js"></script>

		<script>

			var container;
			var camera, scene, renderer,control;
			var obj_url = 'C:/Users/zxh/Desktop/SFMmodelViewer/female/female02.obj',mtl_url = 'C:/Users/zxh/Desktop/SFMmodelViewer/female/female02.mtl';

			init();
			animate();

			function init() {

				container = document.createElement( 'div' );
				container.style.color = '#ffffff';
				document.body.appendChild( container );

				camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
				camera.position.z = 350;
				
				control = new THREE.TrackballControls(camera);
				control.rotateSpeed = 10;

				// scene

				scene = new THREE.Scene();

				var ambient = new THREE.AmbientLight( 0xffffff );
				scene.add( ambient );

				var directionalLight = new THREE.DirectionalLight( 0xffffff );
				directionalLight.position.set( 0, 0, 1 ).normalize();
				directionalLight.intensity = 0.2;
				scene.add( directionalLight );

				//addModel(obj_url,mtl_url);

				renderer = new THREE.WebGLRenderer();
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );
				container.appendChild( renderer.domElement );

				window.addEventListener( 'resize', onWindowResize, false );

				var axisHelper = new THREE.AxisHelper( 50 );
				scene.add( axisHelper );

			}

			function onWindowResize() {
				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();

				renderer.setSize( window.innerWidth, window.innerHeight );

			}

			function animate() {

				requestAnimationFrame( animate );
				render();

			}

			function render() {
				camera.lookAt( scene.position );
				control.update();

				renderer.render( scene, camera );
				var eee = document.getElementsByTagName('01_-_Default1noCulling.JPG');

			}

			// model
			function addModel(objtext,mtltext,imgElements){
				/*
				var onProgress = function ( xhr ) {
					if ( xhr.lengthComputable ) {
						var percentComplete = xhr.loaded / xhr.total * 100;
						console.log( Math.round(percentComplete, 2) + '% downloaded' );
					}
				};

				var onError = function ( xhr ) {
				};

				var loader = new THREE.OBJMTLLoader();
				loader.load( objurl, mtlurl, function ( object ) {

					object.position.y = -80;
					object.position.z = 0;
					scene.add( object );
					control.target.set(0,0,0);
					var light = new THREE.PointLight( 0xffffff );
					light.position.set(5,5,-5);
					light.intensity = 0.6;
					light.distance = 5;
					scene.add( light );

				}, onProgress, onError );*/
				var objmtlLoader = new THREE.OBJMTLLoader();
				var mtlLoader = new THREE.MTLLoader(objmtlLoader.manager);
				var materialsCreator = mtlLoader.parse(mtltext);
				//materialsCreator.preload();

				var object = objmtlLoader.parse( objtext );

				object.traverse( function ( object ) {

					if ( object instanceof THREE.Mesh ) {

						if ( object.material.name ) {
							var name = materialsCreator.materialsInfo[object.material.name].map_kd;
							var img = imgElements[name];
							var material = materialsCreator.create( object.material.name );
							material.map.image = img;
							if ( material ) object.material = material;
						}

					}

				} );

				object.position.y = -80;
				object.position.z = 0;
				scene.add( object );
				control.target.set(0,0,0);
				var light = new THREE.PointLight( 0xffffff );
				light.position.set(5,5,-5);
				light.intensity = 0.6;
				light.distance = 5;
				scene.add( light );
			}

			function filesSelected(files){
				var objtext,mtltext,imgElements={};
				for (var i = 0, f; f = files[i]; i++) {
					var imageReader = new FileReader();
					imageReader.onload = (function(aFile) {
						return function(e) {
							var result = e.target.result;
							if(aFile.name.indexOf('.obj') != -1)
								objtext = result;
							if(aFile.name.indexOf('.mtl') != -1)
								mtltext = result;
							if(aFile.type.indexOf('image') != -1)
							{
								var img = document.createElement('img');
								img.src = result;
								var myCanvas = document.getElementById('myCanvas');
								imgElements[aFile.name] = img;
							}
							if(objtext && mtltext && imgElements)
								addModel(objtext,mtltext,imgElements);
						};
					})(f);
					if(f.type.indexOf('image') != -1)
						imageReader.readAsDataURL(f);
					else
						imageReader.readAsText(f);
				}
			}

			function dropIt(e){
				filesSelected(e.dataTransfer.files);
				e.stopPropagation();
				e.preventDefault();
			}

		</script>

	</body>
</html>
